<%-- 
    Document   : transactions
    Created on : Dec 5, 2011, 4:26:38 AM
    Author     : Michael
--%>
 <%@page import="java.sql.ResultSet"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>

<% if(session.getAttribute("LoggedIn")==null)
       {
    response.sendRedirect("index.html");
    return;
        }

%>
<% if(session.getAttribute("Access").equals("Customer"))
    response.sendRedirect("gohome.jsp");
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Transaction List</title>
<script type="text/javascript">
function row_adclick(txt)
{
    document.forms[txt].submit();
}

function row_mousehover(obj)
{
    obj.style.backgroundColor="#6699FF";
}

function row_mouseout(obj)
{
    obj.style.backgroundColor="";
}

</script>
</head>

<body>
<table width="800" border="1">
<tr>
<td><img src="Logo.jpg" width="800" height="150" />

</td>

</tr>
    <tr><td>        <input type="button" value="Home" onclick="window.location='gohome.jsp'"/>&nbsp;
          <input type="button" value="Log Out" onclick="window.location='logout.jsp'"/></td></tr>
  <tr>
    <td align="center">
        <h2>Transactions</h2></td>
  </tr>
  <tr>
      <td>
      <input type="button" value="Record Transaction" onclick="window.location='transaction.jsp'" />
      <form name="orderby" action="TransactionList.jsp" method="post">
          <label>Order By: </label>
          <input type="radio" name="order" value="date" <% if(request.getParameterNames().hasMoreElements()&&request.getParameter("order").equals("date")) { %>checked="checked" <% } %>
                 onclick="document.orderby.submit()">Date</input>
          <input type="radio" name="order" value="customer" <% if(request.getParameterNames().hasMoreElements()&&request.getParameter("order").equals("customer")) { %>checked="checked" <% } %>
                 onclick="document.orderby.submit()">Customer</input>
          <input type="radio" name="order" value="item" <% if(request.getParameterNames().hasMoreElements()&&request.getParameter("order").equals("item")) { %>checked="checked" <% } %>
                 onclick="document.orderby.submit()">Item</input>
      </form>
      <table width="800" border="1">
        <tr >
        <td >Customer Name</td>
        <td >Item Name</td>
        <td >Units Purchased</td>
        <td >Date</td>

        </tr>
        <% 
            java.sql.Connection conn = null;
            try
            {
                conn = new DBClasses.GetConnection().getConnection();
                String query = "SELECT Concat(c.Firstname, ' ', c.Lastname) AS Name, a.ItemName, s.NumUnits, s.Date FROM customer c, advertisement a, sales s " + 
                        "WHERE c.AccountNo=s.AccountNo AND s.AdID=a.AdvertisementID ";
                
                if(request.getParameterNames().hasMoreElements())
                {
                    if(request.getParameter("order").equals("customer"))
                        query += "ORDER BY c.Firstname";
                    else if(request.getParameter("order").equals("item"))
                        query += "ORDER BY a.ItemName";
                    else
                        query += "ORDER BY s.Date DESC";
                }
                else
                        query += "ORDER BY s.Date DESC";

                java.sql.Statement stmt = conn.createStatement();
                ResultSet rs = stmt.executeQuery(query);

                while(rs.next())
                {

        %>
                <tr >
                <td ><%=rs.getString("Name")%></td>
                <td ><%=rs.getString("a.ItemName")%></td>
                <td ><%=rs.getInt("s.NumUnits")%></td>
                <td ><%=new java.text.SimpleDateFormat("M-d-yyyy").format(rs.getDate("s.Date"))%></td>

                </tr>

        <%
                   }
           }
           catch(Exception e)
           {
               e.printStackTrace();
           }

           finally
           {
               try{
               conn.close(); }
               catch(Exception e) {
                   e.printStackTrace();
               }
           }
        %>
      </table>
      </td>
  </tr>
</table>
</body>
</html>